//
//  ContactUSView.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/4/25.
//

import UIKit
import SwiftyJSON

class ContactUSView: UIView, UITableViewDataSource, UITableViewDelegate {

    var dataSource:Array<JSON>!
    var titleLabStr:String = ""

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.init(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.5)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setDataSource(dataSource:Array<JSON>) {
        self.dataSource = dataSource
        self.setUI()
        self.tableView.reloadData()
    }
    
    func setTitleLabStr(titleLabStr:String) {
        self.titleLabStr = titleLabStr
        self.titleLab.text = titleLabStr
    }
    
    @objc func clickCancelBtn() {
        self.removeFromSuperview()
    }
    
    func setUI()  {
        
        self.addSubview(self.mainView)
        self.mainView.addSubview(self.titleLab)
        self.mainView.addSubview(self.tableView)
        self.addSubview(self.cencelBtn)
        
        self.mainView.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left).offset(15)
            make.right.equalTo(self.snp.right).offset(-15)
            make.height.equalTo(self.dataSource.count * 40 + 100)
            make.centerY.equalTo(self.snp.centerY)
        }
        
        self.titleLab.snp.makeConstraints { (make) in
            make.left.equalTo(self.mainView.snp.left).offset(0)
            make.right.equalTo(self.mainView.snp.right).offset(0)
            make.top.equalTo(self.mainView.snp.top).offset(20)
            make.height.equalTo(30)
        }
        
        self.tableView.snp.makeConstraints { (make) in
            make.left.equalTo(self.mainView.snp.left).offset(0)
            make.right.equalTo(self.mainView.snp.right).offset(0)
            make.top.equalTo(self.titleLab.snp.bottom).offset(10)
            make.height.equalTo(self.dataSource.count * 40 + 100)
        }
        
        self.cencelBtn.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.mainView.snp.centerX)
            make.top.equalTo(self.mainView.snp.bottom).offset(30)
            make.height.equalTo(40)
            make.width.equalTo(40)
        }
        
        if self.titleLabStr.count <= 0 {
            self.titleLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_welcomeUS)
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:ContactUSCell = tableView.dequeueReusableCell(withIdentifier: "ContactUSIdentifier") as! ContactUSCell
        cell.selectionStyle = .none
        cell.callPhoneLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_Call)
        let phoneDic = self.dataSource[indexPath.row]
        let phoneCode:String = phoneDic["phoneCode"].string ?? ""
        let phone:String = phoneDic["phone"].string ?? ""
        cell.phoneTextLab.text = phoneCode as String + phone as String
        let size = (phoneCode + " " + phone).getWidth(UIFont.systemFont(ofSize: 18))
        cell.callPhoneLab.snp.updateConstraints { (make) in
            make.width.equalTo((Constants.screenWidth - 30 - 20 - 56 - size)/2)
        }
        cell.contactUSCellBlock = { cell in
            let telURL:URL = URL.init(string: "tel:" + phoneCode + phone)!
            UIApplication.shared.open(telURL, options:[:]) { (state) in
                
            }
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    lazy var mainView:UIView = {
        let mainView = UIView()
        mainView.backgroundColor = UIColor.white
        mainView.layer.masksToBounds = true
        mainView.layer.cornerRadius = 15
        return mainView;
    }()
    
    lazy var titleLab:UILabel = {
        let titleLab = UILabel()
        titleLab.textColor = UIColor.init(named: "#333333")
        titleLab.font = UIFont.boldSystemFont(ofSize: 22)
        titleLab.textAlignment = .center
        return titleLab;
    }()
    
    lazy var tableView:UITableView = {
        let tableView = UITableView()
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.register(ContactUSCell.self, forCellReuseIdentifier: "ContactUSIdentifier")
        tableView.isScrollEnabled = false
        return tableView;
    }()
    
    lazy var cencelBtn:UIButton = {
        let cencelBtn = UIButton.init(type: .custom)
        cencelBtn.setImage(UIImage.init(named: "login_clear"), for: .normal)
        cencelBtn.addTarget(self, action: #selector(clickCancelBtn), for: .touchUpInside)
        return cencelBtn;
    }()

}
